﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Kay.Cqp.App.Common;
using Kay.Cqp.App.Models;
using Newbe.CQP.Framework.Extensions;
using Newtonsoft.Json;

namespace Kay.Cqp.App.MyForms
{
    public partial class MySettings : Form
    {
        public long Qq { get; set; }

        //public IEnumerable<GroupInfo> Quns { get; set; }

        private readonly MyConsts _c = new MyConsts();

        public MySettings()
        {
            InitializeComponent();
        }

        private void MySettings_Load(object sender, EventArgs e)
        {
            Text = Text + @" - " + Qq;
            //comboBox_Quns.DataSource = Quns;
            LoadQqDate();
        }

        private void LoadQqDate()
        {
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            if (!File.Exists(path))
                return;
            var str = File.ReadAllText(path);
            var qqConfig = JsonConvert.DeserializeObject<QqConfig>(str);
            checkBox_QiyongGuanli.Checked = qqConfig.ManagerIsEnabled;
            checkBox_ZidongTianjiaHaoyou.Checked = qqConfig.AutoAgreeRequestAddFriend;
            checkBox_ZidongRuqun.Checked = qqConfig.AutoAgreeInvitationAddGroup;
            if (qqConfig.Groups == null) return;
            var items = qqConfig.Groups.ToArray();
            comboBox_Quns.Items.AddRange(items);
            if (comboBox_Quns.Items.Count != 0)
                comboBox_Quns.SelectedIndex = 0;
            SaveQqDate();
        }

        private void SaveQqDate()
        {
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), nameof(QqConfig));
            var qqConfig = new QqConfig
            {
                ManagerIsEnabled = checkBox_QiyongGuanli.Checked,
                AutoAgreeRequestAddFriend = checkBox_ZidongTianjiaHaoyou.Checked,
                AutoAgreeInvitationAddGroup = checkBox_ZidongRuqun.Checked,
                Groups = new List<string>()
            };
            foreach (var item in comboBox_Quns.Items)
            {
                qqConfig.Groups.Add(item.ToString());
            }
            var str = JsonConvert.SerializeObject(qqConfig);
            File.WriteAllText(path, str);
        }

        private void checkBox_QiyongGuanli_CheckedChanged(object sender, EventArgs e)
        {
            SaveQqDate();
        }

        private void comboBox_Quns_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGroupConfig();
        }

        private void LoadGroupConfig()
        {
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(GroupConfig));
            if (!File.Exists(path))
                return;
            var str = File.ReadAllText(path);
            var groupConfig = JsonConvert.DeserializeObject<GroupConfig>(str);
            checkBox_QunQiyongGuanli.Checked = groupConfig.ManagerIsEnabled;
            checkBox_ZidongTongyiRuqun.Checked = groupConfig.AutoAgreeAddGroupRequest;
            checkBox_FasongHuanyingYu.Checked = groupConfig.SendWelcome;
            checkBox_ZidongJinyan.Checked = groupConfig.AutoGag;
            checkBox_ZidongTichu.Checked = groupConfig.AutoKick;
            comboBox_Day.SelectedIndex = groupConfig.Day - 1;
            checkBox_BuzaiJieshouJiaqunShenqing.Checked = groupConfig.NotAgreeAddGroupRequest;

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.SendWelcome));
            if (!File.Exists(path))
                return;
            textBox_Huanyingyu.Text = File.ReadAllText(path);

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.GagKeywords));
            if (!File.Exists(path))
                return;
            str = File.ReadAllText(path);
            var jinyanGuanjianzi = JsonConvert.DeserializeObject<Keyworlds>(str);
            if (jinyanGuanjianzi?.Values != null)
                listBox_JinyanGuanjianzi.Items.AddRange(jinyanGuanjianzi.Values.ToArray());

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.KickKeywords));
            if (!File.Exists(path))
                return;
            str = File.ReadAllText(path);
            var tirenGuanjianzi = JsonConvert.DeserializeObject<Keyworlds>(str);
            if (tirenGuanjianzi?.Values != null)
                listBox_JinyanGuanjianzi.Items.AddRange(tirenGuanjianzi.Values.ToArray());
        }

        private void SaveGroupConfig()
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(GroupConfig));
            var groupConfig = new GroupConfig
            {
                ManagerIsEnabled = checkBox_QunQiyongGuanli.Checked,
                AutoAgreeAddGroupRequest = checkBox_ZidongTongyiRuqun.Checked,
                SendWelcome = checkBox_FasongHuanyingYu.Checked,
                AutoGag = checkBox_ZidongJinyan.Checked,
                AutoKick = checkBox_ZidongTichu.Checked,
                Day = (ushort)(comboBox_Day.SelectedIndex + 1),
                NotAgreeAddGroupRequest = checkBox_BuzaiJieshouJiaqunShenqing.Checked
            };
            var str = JsonConvert.SerializeObject(groupConfig);
            File.WriteAllText(path, str);
        }

        private void checkBox_ZidongTongyiRuqun_CheckedChanged(object sender, EventArgs e)
        {
            SaveGroupConfig();
        }

        private void button_QingkongHuanyingyu_Click(object sender, EventArgs e)
        {
            textBox_Huanyingyu.Clear();
        }

        private void button_BaocunHuanyingyu_Click(object sender, EventArgs e)
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            if (string.IsNullOrWhiteSpace(textBox_Huanyingyu.Text))
            {
                ShowInformation("请填写欢迎语！");
                return;
            }
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.SendWelcome));
            File.WriteAllText(path, textBox_Huanyingyu.Text);
        }

        private void button_TianjiaJinyanGuanjianzi_Click(object sender, EventArgs e)
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            if (string.IsNullOrWhiteSpace(textBox_JinyanGuanjianzi.Text))
            {
                ShowInformation("请填写关键字！");
                return;
            }
            listBox_JinyanGuanjianzi.Items.Add(textBox_JinyanGuanjianzi.Text);
            textBox_JinyanGuanjianzi.Clear();
            BaocunJinyinGuanjianzi();
        }

        private void BaocunJinyinGuanjianzi()
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.GagKeywords));
            var keywords = new Keyworlds
            {
                Values = new List<string>()
            };
            foreach (var item in listBox_JinyanGuanjianzi.Items)
            {
                keywords.Values.Add(item.ToString());
            }
            var str = JsonConvert.SerializeObject(keywords);
            File.WriteAllText(path, str);
        }

        private void button_ShanchuJinyanGuanjianzi_Click(object sender, EventArgs e)
        {
            var i = listBox_JinyanGuanjianzi.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择关键字！");
                return;
            }
            listBox_JinyanGuanjianzi.Items.RemoveAt(i);
            BaocunJinyinGuanjianzi();
        }

        private void button_TianjiaTirenGuanjianzi_Click(object sender, EventArgs e)
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            if (string.IsNullOrWhiteSpace(textBox_TirenGuanjianzi.Text))
            {
                ShowInformation("请填写关键字！");
                return;
            }
            listBox_TirenGuanjianzi.Items.Add(textBox_TirenGuanjianzi.Text);
            textBox_TirenGuanjianzi.Clear();
            BaocunTirenGuanjianzi();
        }

        private void BaocunTirenGuanjianzi()
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString());
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text, nameof(_c.KickKeywords));
            var keywords = new Keyworlds
            {
                Values = new List<string>()
            };
            foreach (var item in listBox_TirenGuanjianzi.Items)
            {
                keywords.Values.Add(item.ToString());
            }
            var str = JsonConvert.SerializeObject(keywords);
            File.WriteAllText(path, str);
        }

        private void button_ShanchuTirenGuanjianzi_Click(object sender, EventArgs e)
        {
            var i = listBox_TirenGuanjianzi.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择关键字！");
                return;
            }
            listBox_TirenGuanjianzi.Items.RemoveAt(i);
            BaocunTirenGuanjianzi();
        }

        private void ShowInformation(string msg)
        {
            MessageBox.Show(this, msg, @"提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button_TianjiaQun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox_Qunhao.Text))
            {
                ShowInformation("请填写群号！");
                return;
            }
            var i = comboBox_Quns.Items.IndexOf(textBox_Qunhao.Text);
            if (i != -1)
            {
                comboBox_Quns.SelectedIndex = i;
                return;
            }
            comboBox_Quns.Items.Add(textBox_Qunhao.Text);
            comboBox_Quns.SelectedIndex = comboBox_Quns.Items.Count - 1;
            textBox_Qunhao.Clear();
            SaveQqDate();
        }

        private void button_ShanchuQun_Click(object sender, EventArgs e)
        {
            var i = comboBox_Quns.SelectedIndex;
            if (i == -1)
            {
                ShowInformation("请填选择群！");
                return;
            }
            var path = Path.Combine(_c.AppDirectory, _c.ConfigDirectory, Qq.ToString(), comboBox_Quns.Text);
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }
            comboBox_Quns.Items.RemoveAt(i);
            SaveQqDate();
        }

        private void comboBox_Day_SelectedIndexChanged(object sender, EventArgs e)
        {
            SaveGroupConfig();
        }
    }
}
